/*
 ==============================================================================
Copyright (c) 2007 Amrita Research Labs, Amrita Vishwa Vidyapeetham,
Amritapuri, India. All rights reserved.
This document contains confidential and proprietary information
of Amrita Research Labs. No part of this document may be reproduced
in any form, published, broadcasted or transmitted or have an adaptation
made of it. Any violation will be pursued and enforced to the maximum extent
punishable by law.
==============================================================================
*/

/*! \file config_api.c
 	\brief Config API Source File.
*/

#include <config_api.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <log_api.h>

/*! \brief The log handle. */
extern HLOG _hlog;


/*! \brief Config Open.
 	\param h (OUT)Returns The Opened Handle.
 	\param config_space_name The Name of The Configuration Field.
 	\param mode The Mode of Operation(READONLY or READWRITE)
 	\return 0 on success.
 	\return -EINVAL if Config Space Not Present.
 	\note Refer: stbl_init.
 */
STATUS config_open( HCONFIG *h, const char *config_space_name, uint16 mode )
{
	STATUS status;
	
	if(strcmp(config_space_name, CONFIG_SPACE_NAME) != 0)
	{
		return -EINVAL;
	}
	
	status = stbl_init();
	
	*h = 0;

	return status;
}

/*! \brief Config Close.
 	\param h The Opened Handle.
 	\return 0 on success.
 	\note Refer: stbl_close.
 */
STATUS config_close( HCONFIG h )
{
	STATUS status;

	status = stbl_close();
	
	return status;
}

/* Getter Methods*/

/*! \brief Get configuration Integer Variable.
 	\param h The Opened Handle.
 	\param param The Parameter string table id.
 	\param val (OUT) Returns the Value of the Parameter.
 	\return 0 on success.
 	\return -EINVAL if No Integer value with the given param name present in the config space.
 	\return -EFAULT if stbl_get returned a NULL pointer.
 	\note Refer: stbl_get.
 */
STATUS config_get_int( HCONFIG h, uint32 param, int32 *val )
{
	STATUS status;
	char valbuf[32];
	
	*val = 0;
	status=stbl_get(param, valbuf, sizeof(valbuf));
	if(status >= 0)
	{
		*val = atol(valbuf);
	}
	
	return status;
	
}

/*! \brief Get configuration String Variable.
 	\param h The Opened Handle.
 	\param param The Parameter string table id.
 	\param valbuf (OUT) Returns the Value of the Parameter.
 	\param nvalbuf Size of the value buffer.
 	\return 0 on success.
 	\return -EINVAL if No String value with the given param name present in the config space.
 	\return -EFAULT if stbl_get returned a NULL pointer.
 	\note Refer: stbl_get.
 */
STATUS config_get_str	( HCONFIG h, uint32 param, char *valbuf, int nvalbuf)
{

	return stbl_get(param, valbuf, nvalbuf);
	
}

/* Setter Methods */

/*! \brief Set configuration Integer Variable.
 	\param h The Opened Handle.
 	\param param The Parameter string table id.
 	\param val The Value of the Parameter.
 	\return 0 on success.
 	\return -EINVAL if No Integer value with the given param name present in the config space.
 	\note Refer: stbl_put.
 */
STATUS config_set_int( HCONFIG h, uint32 param, int32 val )
{
	STATUS status;
	
	int8 buffer[32];
	memset(buffer,0,sizeof(buffer));
	str_snprintf( buffer, sizeof(buffer), "%d", val );
	status=stbl_put(param, buffer);
	
	return status;
}


/*! \brief Set configuration String Variable.
 	\param h The Opened Handle.
 	\param param The Parameter string table id.
 	\param val The Value of the Parameter.
 	\return 0 on success.
 	\return -EINVAL if No String value with the given param name present in the config space.
 	\note Refer: stbl_put.
 */
STATUS config_set_str( HCONFIG h, uint32 param, const char *val )
{
	STATUS status;
	
	status=stbl_put(param, val);

	return status;	
}
